#include "CCpu.h"

CCpu::CCpu()
{

}
std::string CCpu::getCpuMhz()
{
	int fd = open(cpuPath.c_str(), O_RDONLY);
	ssize_t readBytes = 0;
	if(fd != -1)
	{
		char buffer[10]{};
		readBytes = read(fd, &buffer, sizeof(buffer));
		if(readBytes != -1)
		{
			std::string frequency(buffer);
			frequency.erase(std::remove(frequency.begin(), frequency.end(), '\n'), frequency.end());
			frequency += " Hz";
			cpuFreqMhz = frequency;
			close(fd);
			return cpuFreqMhz;
			
		}
		LOG(WARNING) << "Cannot open file";
		return "Cannot open file";
	}
	LOG(ERROR) << "Undefined error";
	return " ";
}
std::string CCpu::getCpuGhz()
{
	int fd = open(cpuPath.c_str(), O_RDONLY);
	ssize_t readBytes = 0;
	if(fd != -1)
	{
		char buffer[10]{};
		readBytes = read(fd, buffer, sizeof(buffer));
		if(readBytes != -1)
		{
			std::string frequency(buffer);
			frequency.erase(std::remove(frequency.begin(), frequency.end(), '\n'), frequency.end());
			std::istringstream iss(frequency);
			float frequencyTemp = 0;
			iss >> frequencyTemp;
			frequencyTemp /= 1000000;
			std::string afterConv = std::to_string(frequencyTemp);
			afterConv += " Ghz";
			cpuFreqGhz = afterConv;
			close(fd);
			return cpuFreqGhz;
		}
		LOG(WARNING) << "Cannot open file";
		return "Cannot open file";
	}
	perror(cpuPath.c_str());
	LOG(ERROR) << "Undefined error";
	return " ";
}
CCpu::~CCpu()
{

}